package com.schoolvisitors.controller.user;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.schoolvisitors.response.ResultBody;
import com.schoolvisitors.service.SystemConfigService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/user")
public class HomeController {
    private final SystemConfigService systemConfigService;

    public HomeController(SystemConfigService systemConfigService) {
        this.systemConfigService = systemConfigService;
    }

    @GetMapping("/getHomeInfoVersion")
    public ResultBody getHomeInfoVersion() {
        Map<String, Long> map = new HashMap<>();
        map.put("version", systemConfigService.getSystemConfigVersion());
        return ResultBody.success(map);
    }

    @GetMapping("/getHomeInfo")
    public ResultBody getHomeInfo() {
        JSONObject data = new JSONObject();
        String notice = systemConfigService.getSystemConfigToString("notice");
        String swipeTop = systemConfigService.getSystemConfigToString("swipeTop");
        String swipeBottom = systemConfigService.getSystemConfigToString("swipeBottom");
        JSONArray swipeTopArr = JSON.parseArray(swipeTop);
        JSONArray swipeBottomArr = JSON.parseArray(swipeBottom);
        data.put("notice", notice);
        data.put("swipeTop", swipeTopArr);
        data.put("swipeBottom", swipeBottomArr);
        data.put("checkBeforeAfterTime", systemConfigService.getSystemConfigToLong("checkBeforeAfterTime"));
        data.put("advanceReservationDay", systemConfigService.getSystemConfigToLong("advanceReservationDay"));
        return ResultBody.success(data);
    }
}
